﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabMS.Web.Equipment.LowValue
{
    public partial class RepairSetting : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("DZPWX");//必须写在ispostback外
            strPageUserType.Add("mana");
            if (!Page.IsPostBack)
            {
                ddlbind();
                if (Request.QueryString["ID"]==null)
                {
                    Common.JShelper.JSAlert(Page, "Err", "参数错误");
                    return;
                }
                else
                {
                    strID.Text = Request.QueryString["ID"].ToString();
                    bind(strID.Text);
                }
            }

            ddlLab.Attributes.Add("readOnly", "-1");
            tbisntrumentname.Attributes.Add("readOnly", "-1");
            tbinstrumentNum.Attributes.Add("readOnly", "-1");
            tbApplyer.Attributes.Add("readOnly", "-1");
            tbReportTime.Attributes.Add("readOnly", "-1");
            tbRepairTime.Attributes.Add("readOnly", "-1");
            tbRepairTime.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
        }


        protected void ddlbind()
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLab.DataSource = mLab;
            ddlLab.DataTextField = "Lab_Name";
            ddlLab.DataValueField = "Lab_Code";
            ddlLab.DataBind();

        }
        protected void bind(string strid)
        {
            BLL.GoodsMaintain bgoodsmaintain = new LabMS.BLL.GoodsMaintain();
            Model.GoodsMaintain goodsmaintain = bgoodsmaintain.GetModel(Convert.ToInt32(strid));
            ddlLab.SelectedValue = goodsmaintain.LabName;
            tbManager.Text = goodsmaintain.AssetManagers;
            tbTelNum.Text = goodsmaintain.Tel;
            tbinstrumentNum.Text = goodsmaintain.Numbers;
            tbisntrumentname.Text = goodsmaintain.Name;
            tbIsInTime.SelectedValue = goodsmaintain.Guarantee;
            tbApplyer.Text = goodsmaintain.Applicant;
            tbReportTime.Text = goodsmaintain.ReportedTime.ToString();
            tbDescription.Text = goodsmaintain.Describe;

            tbLocation.Text = goodsmaintain.StorageSites;
        }

        private void savedata(string status)
        {
            BLL.GoodsMaintain bgoodsmaintain = new LabMS.BLL.GoodsMaintain();
            Model.GoodsMaintain goodsmaintain = bgoodsmaintain.GetModel(Convert.ToInt32(strID.Text));
            goodsmaintain.MCo = tbRepairGroup.Text;
            goodsmaintain.MLinkman = tbTransfer.Text.Trim();
            goodsmaintain.MPhone = tbPhoneNum.Text;
            goodsmaintain.MComment = tbComments.Text;
            if (!tbRepairTime.Text.Trim().Equals(""))
            {
                goodsmaintain.MDate = DateTime.Parse(tbRepairTime.Text);
            }
            goodsmaintain.LabName = ddlLab.SelectedValue;
            goodsmaintain.AssetManagers = tbManager.Text;
            goodsmaintain.Tel = tbTelNum.Text;
            goodsmaintain.Guarantee = tbIsInTime.SelectedValue;
            goodsmaintain.Applicant = tbApplyer.Text.Trim();
            goodsmaintain.Describe = tbDescription.Text;
            goodsmaintain.Status = status;

            BLL.InstrumentAndConsumables biac = new LabMS.BLL.InstrumentAndConsumables();
            Model.InstrumentAndConsumables iac;
            if (biac.GetModelList(" Numbers=" + tbinstrumentNum.Text.Trim()).Count != 0)
            {
                iac = biac.GetModelList(" Numbers=" + tbinstrumentNum.Text.Trim())[0];
                try
                {
                    bgoodsmaintain.Update(goodsmaintain);
                }
                catch (Exception e)
                {
                    Common.JShelper.JSAlert(Page, "Err", status + "失败");
                    return;
                }
                iac.Status = status;
                biac.Update(iac);
                Common.JShelper.JSAlertAndRedirect(Page, "Err", status + "成功", "lowrepairlist.aspx");
            }
            else
            {
                Common.JShelper.JSAlert(Page, "Err", status + "失败");
                return;
            }


        }

        protected void lbRepaired_Click(object sender, EventArgs e)
        {
            if (tbinstrumentNum.Text.Trim().Equals(""))
            {
                Common.JShelper.JSAlert(Page, "Err", "请确定选中了元件");
                return;
            }
            else
            {
                savedata("修复");
            }
        }

        protected void lbDiscard_Click(object sender, EventArgs e)
        {
            if (tbinstrumentNum.Text.Trim().Equals(""))
            {
                Common.JShelper.JSAlert(Page, "Err", "请确定选中了元件");
                return;
            }
            else
            {
                savedata("报废");
            }
        }
    }
}
